#include "QQSql.h"

QX_REGISTER_CPP(QQFriend)
QX_REGISTER_CPP(QQMessage)

namespace qx
{
	template <> void register_class(QxClass<QQFriend>& t)
	{
		t.id(&QQFriend::id, "id");
		t.data(&QQFriend::account, "account");
		t.data(&QQFriend::nickname, "nickname");
		t.data(&QQFriend::icon, "icon");
		t.data(&QQFriend::keyPoints, "keyPoints");
	}

	template <> void register_class(QxClass<QQMessage>& t)
	{
		t.id(&QQMessage::id, "id");
		t.data(&QQMessage::name, "name");
		t.data(&QQMessage::avatar, "avatar");
		t.data(&QQMessage::sender, "sender");
		t.data(&QQMessage::receiver, "receiver");
		t.data(&QQMessage::time, "time");
		t.data(&QQMessage::content, "content");
		t.data(&QQMessage::type, "type");
		t.data(&QQMessage::senderType, "senderType", 0, true, false);
	}
}

QQSql::QQSql(QObject* parent)
	: QObject(parent)
{
}

QQSql::~QQSql()
{
}

QQSql* QQSql::get_instance()
{
	static QQSql* instance = nullptr;
	if (instance == nullptr)
		instance = new QQSql(qApp);
	return instance;
}

void QQSql::init()
{
	qx::QxSqlDatabase::getSingleton()->setDriverName("QSQLITE");
	qx::QxSqlDatabase::getSingleton()->setDatabaseName("qq.db");
	qx::dao::create_table<QQFriend>();
	qx::dao::create_table<QQMessage>();
}

QQFriend QQSql::get_friend_info_by_account(const QString& account)
{
	qx::QxSqlQuery query;
	query.where("account").isEqualTo(account);
	QQFriend friend_info;
	friend_info.account = account;
	qx::dao::fetch_by_id(friend_info);
	return friend_info;
}

QList<QQFriend> QQSql::get_friend_list()
{
	QList<QQFriend> friend_list;
	qx::dao::fetch_all(friend_list);
	return friend_list;
}

QString QQSql::get_friend_icon_by_account(const QString& account)
{
	QQFriend friend_info;
	friend_info.account = account;
	qx::dao::fetch_by_id(friend_info);
	return friend_info.icon;
}

QString QQSql::get_friend_nickname_by_account(const QString& account)
{
	QQFriend friend_info;
	friend_info.account = account;
	qx::dao::fetch_by_id(friend_info);
	return friend_info.nickname;
}

void QQSql::add_friend(QQFriend& friend_info)
{
	qx::dao::insert(friend_info);
}

void QQSql::delete_friend(const QString& account)
{
	QQFriend* friend_info = new QQFriend;
	friend_info->account = account;
	qx::dao::delete_by_id(friend_info);
}

QQMessage QQSql::get_message_info_by_id(long id)
{
	QQMessage message_info;
	message_info.id = id;
	qx::dao::fetch_by_id(message_info);
	return message_info;
}

QList<QQMessage> QQSql::get_message_list_by_sender(const QString& sender)
{
	QList<QQMessage> message_list;
	qx::QxSqlQuery query;
	query.where("sender").isEqualTo(sender);
	qx::dao::fetch_by_query(query, message_list);
	return message_list;
}

QList<QQMessage> QQSql::get_message_list_by_receiver(const QString& receiver)
{
	QList<QQMessage> message_list;
	qx::QxSqlQuery query;
	query.where("receiver").isEqualTo(receiver);
	qx::dao::fetch_by_query(query, message_list);
	return message_list;
}

QList<QQMessage> QQSql::get_message_list_by_sender_and_receiver(const QString& sender, const QString& receiver)
{
	QList<QQMessage> message_list;
	qx::QxSqlQuery query;
	query.where("sender").isEqualTo(sender).and_("receiver").isEqualTo(receiver);
	qx::dao::fetch_by_query(query, message_list);
	return message_list;
}

void QQSql::add_message(QQMessage& message_info)
{
	qx::dao::insert(message_info);
}

void QQSql::delete_message(long id)
{
	QQMessage* message_info = new QQMessage;
	message_info->id = id;
	qx::dao::delete_by_id(message_info);
}
